let city = ''
$.ajax({
  url: "../city.json",
  data: { action: "jQueryAjax" },
  type: "GET",
  async: false,
  success: function (data) {
    city = data
  },
  //返回数据的格式
  dataType: "json"
})
const enterpriseVue = new Vue({
  el: '#app',
  data() {
    return {
      rules: {
        Supplier_Name: [{ required: true, message: '请输入供应商名称', trigger: 'blur' }],
        Tax_Number: [{ required: true, message: '请输入纳税识别号', trigger: 'blur' }],
        selectProvince: [{ required: true, message: '请选择供应商地址', trigger: 'blur' }],
      },
      Bank_Name: [{ required: true, message: '请输入供应商开户银行', trigger: 'blur' }],
      Account_Number: [{ required: true, message: '请输入供应商银行账号', trigger: 'blur' }],
      brand: [],
      article: [],
      text: [],
      // 整个省市县数据
      city,
      // 被选中的市数据
      cityList: [],
      // 被选中的县数据
      areaList: [],
      isdisabled: true,
      selectProvince: {},
      selectCity: {},
      selectArea: {},
      address: '',
      dialogImageUrl: '',
      dialogVisible: false,
      fileList: [],
      form: {
        Supplier_SerialNumber: '',
        Supplier_Name: '',
        Tax_Number: '',
        Supplier_Address: '',
        Enterprise_Info_SerialNumber: '',
        Enterprise_Info_SerialNumber: $.cookie("enterprise"),
        Children: {
          supplier_brand: [{       //供应商经营品牌
            Supplier_Brand_SerialNumber: "",
            Supplier_SerialNumber: '',
            Brand_SerialNumber: '',
          }],
          supplier_article: [{       //供应商可供商品
            Supplier_Article_SerialNumber: "",
            Supplier_SerialNumber: '',
            Article_SerialNumber: '',
          }],
          supplier_bank_account: [{       //供应商银行账号
            Supplier_Bank_Account_SerialNumber: "",
            Supplier_SerialNumber: '',
            Bank_Name: '',
            Account_Number: '',
          }],
          supplier_bank_pic: []
        }
      },
      loading: false,
      disabled: true
    }
  },
  mounted() {
    this.form.Enterprise_Info_SerialNumber = $.cookie("enterprise");
    this.getData();
  },
  methods: {
    getData() {
      let _this = this;
      $.ajax({
        url: DomainName + '/GoodsFileInquiry',
        type: 'POST',
        data: {
          Data: "brand",
        },
        success: function (res) {
          res = res.filter(function (item) {
            return item.Row_Status != '1'
          })
          // console.log(res);
          for (let i = 0; i < res.length; i++) {
            let obj = {}
            obj.Brand_SerialNumber = res[i].Brand_SerialNumber;
            obj.Brand_Name = res[i].Brand_Name;
            _this.brand.push(obj);
          }
          // console.log(_this.brand);
        },
        error: function () {
          _this.$message.error("操作失败，请稍后再试")
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
      $.ajax({
        url: DomainName + '/getInquiryAction',
        type: 'POST',
        data: {
          Data: "article_info",
          token: $.cookie("token")
        },
        success: function (res) {
          for (let i = 0; i < res.length; i++) {
            _this.article.push({
              Article_SerialNumber: res[i].Article_SerialNumber,
              Article_Name: res[i].Article_Name
            });
          }
          console.log(_this.article);
        },
        error: function () {
          _this.$message.error("操作失败，请稍后再试")
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
    },
    submitForm(formName) {
      let _this = this
      console.log(_this.form)
      // if (_this.address == '' || _this.form.Children.supplier_brand[0].Brand_SerialNumber == '' || _this.form.Children.supplier_article[0].Article_SerialNumber == '' || _this.form.Children.supplier_bank_account[0].Bank_Name == '' || _this.form.Children.supplier_bank_account[0].Account_Number == '') {
      //   return _this.$message.error("请完善信息")
      // }
      this.$refs[formName].validate((valid) => {
        if (valid) {
          _this.loading = true
          _this.fileList.forEach(function (item, index) {
            _this.form.Children.supplier_bank_pic.push(item);
          })
          $.ajax({
            url: DomainName + '/getAddAction',
            type: 'POST',
            data: {
              Data: JSON.stringify({
                supplier_info: [_this.form]
              }),
              token: $.cookie("token")
            },
            success: function (res) {
              if (res.Status === 'success') {
                _this.$message.success("提交成功")
                window.location.href = './Supplier.html'
              } else {
                _this.$message.error("提交失败，请稍后再试")
              }
            },
            error: function () {
              _this.$message.error("操作失败，请稍后再试")
            },
            complete: function (XMLHttpRequest) {
              _this.loading = false
              if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
              }
            },
          })
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //供应商相关材料照片
    upload(file) {
      let _this = this
      new Compressor(file, {
        quality: 0.08,
        success(file) {
          const formData = new FormData();
          formData.append('file', file, file.name);
          let oFileReader = new FileReader();
          oFileReader.readAsDataURL(file);
          oFileReader.onload = function (e) {
            $.ajax({
              type: 'POST',
              url: DomainName + '/FileList',
              data: {
                Data: e.target.result,
                DataLength: file.size,
                FileName: file.name
              },
              complete: function (XMLHttpRequest) {
                _this.loading = false
                if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                  window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                }
              },
              success: function (Result) {
                if (Result.Status == "success") {
                  // _this.fileList[_this.fileList.length - 1].pic = e.target.result
                  _this.fileList.push({
                    Supplier_Bank_Pic_SerialNumber: '',
                    Supplier_SerialNumber: '',
                    Picture_Order: _this.fileList.length + 1,
                    Picture_Url: '',
                  })
                  _this.fileList[_this.fileList.length - 1].Picture_Url = Result.Picture_Url
                  _this.disabled = false;
                } else {
                  _this.$message.error(Result.msg)
                }
              },
              error: function () {
                _this.$message.error("操作失败，请稍后再试")
              },
              dataType: "json"
            });
          }
        },
        error(err) {
          console.log(err.message);
        },
      });
    },
    //查看图片
    handlePictureCardPreview(file) {
      this.dialogVisible = true
      this.dialogImageUrl = file
    },
    //删除图片
    handleRemove(index) {
      this.fileList = this.fileList.filter(function (item) {
        return item.Picture_Order != index
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    add_supplier_brand() {
      this.form.Children.supplier_brand.push({       //供应商经营品牌
        Supplier_Brand_SerialNumber: "",
        Supplier_SerialNumber: '',
        Brand_SerialNumber: '',
      })
    },
    remove_supplier_brand() {
      if (this.form.Children.supplier_brand.length > 1) {
        this.form.Children.supplier_brand.pop()
      } else {
        this.$message({
          message: '不能在删除了',
          type: 'warning'
        })
      }
    },
    add_supplier_bank_account() {
      this.form.Children.supplier_bank_account.push({       //供应商银行账号
        Supplier_Bank_Account_SerialNumber: "",
        Supplier_SerialNumber: [],
        Bank_Name: '',
        Account_Number: '',
      })
    },
    remove_supplier_bank_account() {
      if (this.form.Children.supplier_bank_account.length > 1) {
        this.form.Children.supplier_bank_account.pop()
      } else {
        this.$message({
          message: '不能在删除了',
          type: 'warning'
        })
      }
    },
    add_supplier_article() {
      this.form.Children.supplier_article.push({       //供应商可供商品
        Supplier_Article_SerialNumber: "",
        Supplier_SerialNumber: '',
        Article_SerialNumber: '',
      })
    },
    remove_supplier_article() {
      if (this.form.Children.supplier_article.length > 1) {
        this.form.Children.supplier_article.pop()
      } else {
        this.$message({
          message: '不能在删除了',
          type: 'warning'
        })
      }
    },
    // 省份 市 县联动
    selectProvinceFun(event) {
      console.log(event)
      if (event) {
        this.cityList = event.children
      } else {
        this.cityList = []
      }
      this.areaList = []
    },
    selectCityFun(event) {
      console.log(event)
      if (event) {
        this.areaList = event.children
      } else {
        this.areaList = []
      }
    },
    selectAreaFun(event) {
      if (event) {
        console.log(event)
        this.isdisabled = false
      } else {
        this.isdisabled = true
      }
    },
    selectAddress(event) {
      if (event && event != '' && this.selectArea.label != undefined) {
        this.form.Supplier_Address = this.selectProvince.label + '/'
          + this.selectCity.label + '/'
          + this.selectArea.label + '/'
          + this.address
      }
    }
  }
})